package com.zzx.test.future;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.NumberFormat;
import java.util.Locale;

public class TwelveClass1 {

    @Test
    public void testSwitch() {
        switchJava12("abc");
    }

    public static void switchJava12(String day) {
        String season = switch (day) {
            case "march", "april", "may" -> "春天";
            case "june", "july", "august" -> "夏天";
            case "september", "october", "november" -> "秋天";
            case "december", "january", "february" -> "冬天";
            default -> "day error";
        };
        System.out.println("当前季节是:" + season);
    }

    @Test
    public void fileMismatchTest() {
        fileMismatch();
    }

    public static void fileMismatch() {
        try {
            // 创建两个文件
            Path pathA = Files.createFile(Paths.get("a.txt"));
            Path pathB = Files.createFile(Paths.get("b.txt"));

            // 写入相同内容
            Files.write(pathA, "abc".getBytes(), StandardOpenOption.WRITE);
            Files.write(pathB, "abc".getBytes(), StandardOpenOption.WRITE);
            long mismatch = Files.mismatch(pathA, pathB);
            // 文件内容完全一致返回-1，不同就返回第一个不同的字符位置
            System.out.println(mismatch);

            // 追加不同内容
            Files.write(pathA, "123".getBytes(), StandardOpenOption.APPEND);
            Files.write(pathB, "321".getBytes(), StandardOpenOption.APPEND);
            mismatch = Files.mismatch(pathA, pathB);
            System.out.println(mismatch);

            // 删除创建的文件
            pathA.toFile().deleteOnExit();
            pathB.toFile().deleteOnExit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void numberDisplay() {
        // 数字显示格式可以修改
        System.out.println("Compact Formatting is:");
        NumberFormat upvotes = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT);

        System.out.println(upvotes.format(100));
        System.out.println(upvotes.format(1000));
        System.out.println(upvotes.format(10000));
        System.out.println(upvotes.format(100000));
        System.out.println(upvotes.format(1000000));

        // 设置小数位数
        upvotes.setMaximumFractionDigits(1);
        System.out.println(upvotes.format(1234));
        System.out.println(upvotes.format(123456));
        System.out.println(upvotes.format(12345678));

    }

}
